package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.po.DtAuthGroup;
import com.xueyi.aixihu.entity.vo.DtAuthGroupTitleVo;
import com.xueyi.aixihu.entity.vo.LocalDtAuthGroupVo;
import com.xueyi.aixihu.entity.vo.SelectVo;
import com.xueyi.aixihu.reqeust.AddAuthGroupReq;
import com.xueyi.aixihu.reqeust.FindAllAuthGroupReq;
import org.apache.ibatis.annotations.*;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;

/**
 * @author lvyi
 * @date : 2022/8/17
 * @Description : 角色表的mapper接口
 */
public interface LocalDtAuthGroupMapper {

    /**
     * 查询所有角色
     *
     * @return 角色名
     */
    @Select(" SELECT id,title name FROM dt_auth_group WHERE status = 1 ")
    List<SelectVo> findAllAuthGroupTitle();


    /**
     * 查询出所有角色信息 以及关联其角色的条数
     *
     * @return
     */
    @Select(" <script> " +
            "select a1.* from (" +
            " SELECT " +
            " ag.* , COUNT(du.id) size " +
            " FROM " +
            " dt_auth_group ag " +
            " LEFT JOIN dt_auth_group_access aga ON aga.group_id = ag.id " +
            " LEFT JOIN dt_users du ON du.id = aga.uid " +
            " WHERE " +
            " ag.status = 1 " +
            " <if test='name != null' > " +
            "  AND ag.title LIKE '%${name}%' " +
            " </if> " +
            " GROUP BY ag.id " +
            " ORDER BY size DESC " +
            ") a1"+
            " LIMIT #{startIndex},#{limit} " +
            " </script> ")
    List<LocalDtAuthGroupVo> findAllAuthGroup(FindAllAuthGroupReq findAllAuthGroupReq);


    @Select(" <script> " +
            " SELECT " +
            " COUNT(ag.id) " +
            " FROM " +
            " dt_auth_group ag " +
            " WHERE " +
            " ag.status = 1 " +
            " <if test='name != null' > " +
            "  AND ag.title LIKE '%${name}%' " +
            " </if> " +
            " </script> ")
    Integer findAllAuthGroupTotal(FindAllAuthGroupReq findAllAuthGroupReq);

    /**
     *  查询名字是否相同
     * @param name
     * @return
     */
    @Select(" SELECT id FROM dt_auth_group WHERE title = #{name} AND status = 1 ")
    Integer findIsSame(String name);

    /**
     * 编辑角色的名称
     *
     * @param id   角色id
     * @param name 名称
     */
    @Update(" UPDATE dt_auth_group SET title = #{name} WHERE id = #{id} ")
    void updateAuthName(Integer id, String name);


    /**
     * 新增角色
     *
     * @param addAuthGroupReq
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert(" INSERT INTO dt_auth_group (title,status,rules,create_time) VALUES (#{title},1,null,#{time}) ")
    void addAuthGroup(AddAuthGroupReq addAuthGroupReq);

    /**
     * 修改角色的状态
     *
     * @param id     角色id
     * @param status 角色状态
     */
    @Update(" UPDATE dt_auth_group SET status = #{status} WHERE id = #{id} ")
    void updateAuthStatus(Integer id, Integer status);

    /**
     * 根据角色id查询其信息
     *
     * @param id
     * @return
     */
    @Select(" SELECT * FROM dt_auth_group WHERE id = #{id} AND status = 1 ")
    DtAuthGroup findRoleById(Integer id);


    /**
     * 删除角色的同时解除绑定了该角色的用户
     * @param id
     */
    @Delete(" DELETE FROM dt_auth_group_access WHERE group_id = #{id} ")
    void deleteUserByRole(Integer id);
}
